package cn.itcast.tanhua.mytanhuasso.controller;

import cn.hutool.core.util.StrUtil;
import cn.itcast.tanhua.constant.MessageConstant;

import cn.itcast.tanhua.mytanhuasso.service.UserService;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 林金洋
 * @since 2021/12/6 16:07
 */
@RestController
@RequestMapping("user")
@Slf4j
public class ReleaseController {

    @Autowired
    private UserService userService;

    /**
     * 用户解禁
     *
     * @param user
     * @return
     */
    @PostMapping("release/on")
    public ResponseEntity<Object> releaseOn(@RequestBody Map<String,Long> user){
        Long userId = user.get("userId");
        Map<String, Object> rmap = this.userService.releaseOn(userId);
        if (null != rmap){
            // 返回验证成功
            return ResponseEntity.ok(rmap);
        }
        // 返回验证失败
        ErrorResult errorResult = ErrorResult.builder()
                .errCode(MessageConstant.LOGIN_VERIFY_ERROR_CODE)
                .errMessage(MessageConstant.LOGIN_VERIFY_ERROR)
                .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 用户禁用
     *
     * @param user
     * @return
     */
    @PostMapping("release/down")
    public ResponseEntity<Object> releaseDown(@RequestBody Map<String,Long> user){
        Long userId = user.get("userId");
        Map<String, Object> rmap = this.userService.releaseDown(userId);
        if (null != rmap){
            // 返回验证成功
            return ResponseEntity.ok(rmap);
        }
        // 返回验证失败
        ErrorResult errorResult = ErrorResult.builder()
                .errCode(MessageConstant.LOGIN_VERIFY_ERROR_CODE)
                .errMessage(MessageConstant.LOGIN_VERIFY_ERROR)
                .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
